TWB.New_Module({
	///////////////////////////
	name : "ReportOptions",  //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	settings : {
		reportsize : {
			type : "text"
		}
	},
	ReportCache : function(){
		window.openDialog("chrome://twbooster/content/report_cache.xul", "","modal,chrome,dialog,resizable=yes", TWB.lang("rcache"), window);
	},
	OpenReports : function(){
		if(TWB.Modules.Icons.Screen()=="report"){
			var world=TWB.World(true);
			if(typeof TWB.Servers[world.replace(/\d+/,"")]=="undefined"){
				return alert(TWB.lang("servernotsup"));
			}
			var reps=TWB.Xpath("//td/input[contains(@name,'id_')]",6);
			var i;
			for(i=0; i<reps.length; i++){
				if(reps[i].checked){
					TWB.Load_Master_Url("http://"+world+"."+TWB.Servers[world.replace(/\d+/,"")]+"/game.php?screen=report&mode=all&view="+reps[i].getAttribute("name").split("_")[1],true);
				}
			}
			if(reps.length==0){
				alert(TWB.lang("howtoor"));
			}
		}
		else{
			TWB.Processor.start("router","Modules.ReportOptions.OpenReports","*",true);
			TWB.Modules.Icons.GoTo("report");
		}
	},
	SameTroops : function(){
		if(TWB.Modules.Icons.Screen()=="report" && TWB.Modules.Icons.Mode()=="all" && TWB.URL().match(/view=/)!=null){
			var tab=TWB.Xpath("//table[@id='attack_info_att_units']/tbody/tr",6);
			var Cvil=TWB.Xpath("//table[@id='attack_info_att']/tbody/tr[2]/td[2]/a",9).href.match(/id=(\d+)/)[1];
			var Tvil=TWB.Xpath("//table[@id='attack_info_def']/tbody/tr[2]/td[2]/a",9).href.match(/id=(\d+)/)[1];
			tab[0]=TWB.filterTN(tab[0].childNodes);
			tab[1]=TWB.filterTN(tab[1].childNodes);
			TWB.sametroops={};
			for(i=1; i<tab[0].length; i++){
				TWB.sametroops[tab[0][i].childNodes[0].getAttribute("src").match(/unit_(\w+)\./)[1]]=tab[1][i].innerHTML;
			}
			TWB.Processor.start("sametroops","Modules.ReportOptions.SameTroops_Launch","*",true);
			TWB.Load_Url(TWB.Modules.Villages.MakeUrl(Cvil,"place",undefined,true)+"&target="+Tvil);
		}
		else{
			alert(TWB.lang("gotoreport"));
		}
	},
	SameTroops_Launch : function(){
		if(typeof TWB.sametroops!="undefined"){
			var unit;
			for(unit in TWB.sametroops){
				TWB.Xpath("//input[@name='"+unit+"']",9).value=TWB.sametroops[unit];
			}
			TWB.Xpath("//input[@type='submit']",9).click();
			delete TWB.sametroops;
		}
	},
	AllTroops : function(){
		if(TWB.Modules.Icons.Screen()=="report" && TWB.Modules.Icons.Mode()=="all" && TWB.URL().match(/view=/)!=null){
			var Cvil=TWB.Xpath("//table[@id='attack_info_att']/tbody/tr[2]/td[2]/a",9).href.match(/id=(\d+)/)[1];
			var Tvil=TWB.Xpath("//table[@id='attack_info_def']/tbody/tr[2]/td[2]/a",9).href.match(/id=(\d+)/)[1];
			TWB.alltroops=true;
			TWB.Processor.start("alltroops","Modules.ReportOptions.AllTroops_Launch","*",true);
			TWB.Load_Url(TWB.Modules.Villages.MakeUrl(Cvil,"place",undefined,true)+"&target="+Tvil);
		}
		else{
			alert(TWB.lang("gotoreport"));
		}
	},
	AllTroops_Launch : function(){
		if(typeof TWB.alltroops!="undefined"){
			var WD=content.window;
			if(typeof WD.wrappedJSObject!="undefined"){
				WD=WD.wrappedJSObject;
			}
			WD.selectAllUnits(true);
			TWB.Xpath("//input[@type='submit']",9).click();
			delete TWB.alltroops;
		}
	},
	Convert : function(){
		if(TWB.Modules.Icons.Screen()=="report" && TWB.Modules.Icons.Mode()=="all" && TWB.URL().match(/view=/)!=null){
			var world=TWB.World(true);
			if(typeof TWB.Servers[world.replace(/\d+/,"")]=="undefined"){
				return alert(TWB.lang("servernotsup"));
			}
			var i;
			var Tag;
			var SS=TWB.gei("TWB-Servers").childNodes;
			for(i=0; i<SS.length; i++){
				if(SS[i].getAttribute("name")==world.replace(/\d+/,"")){
					Tag=SS[i].getAttribute("id");
					break;
				}
			}
			var WD=content.window;
			if(typeof WD.wrappedJSObject!="undefined"){
				WD=WD.wrappedJSObject;
			}
			WD=WD.getSelection();
			if(new String(WD).length>0){
				TWB.convert=WD;
				TWB.Processor.start("convert","Modules.ReportOptions.Convert_Launch","*",true);
				TWB.Load_Master_Url("http://www.mytwstats.com/tool-convert.php?action=language&id="+Tag);
			}
			else{
				alert(TWB.lang("selectrep"));
			}
		}
		else{
			alert(TWB.lang("gotoreport"));
		}
	},
	Convert_Launch : function(){
		if(typeof TWB.convert!="undefined"){
			var D=_window("mytwstats").document;
			TWB.Xpath("//*[@name='report']",9,D).value=TWB.convert;
			TWB.Xpath("//*[@type='submit']",6,D)[2].click();
			delete TWB.convert;
		}
	},
	Simulate : function(){
		if(TWB.Modules.Icons.Screen()=="report" && TWB.Modules.Icons.Mode()=="all" && TWB.URL().match(/view=/)!=null){
			var tab=TWB.Xpath("//table[@id='attack_info_att_units']/tbody/tr",6);
			var tab2=TWB.Xpath("//table[@id='attack_info_def_units']/tbody/tr",6);
			var Cvil=TWB.Xpath("//table[@id='attack_info_att']/tbody/tr[2]/td[2]/a",9).href.match(/id=(\d+)/)[1];
			var Tvil=TWB.Xpath("//table[@id='attack_info_def']/tbody/tr[2]/td[2]/a",9).href.match(/id=(\d+)/)[1];
			tab[0]=TWB.filterTN(tab[0].childNodes);
			tab[1]=TWB.filterTN(tab[1].childNodes);
			tab2[1]=TWB.filterTN(tab2[1].childNodes);
			TWB.simulate=[{},{}];
			for(i=1; i<tab[0].length; i++){
				TWB.simulate[0][tab[0][i].childNodes[0].getAttribute("src").match(/unit_(\w+)\./)[1]]=tab[1][i].innerHTML;
				TWB.simulate[1][tab[0][i].childNodes[0].getAttribute("src").match(/unit_(\w+)\./)[1]]=tab2[1][i].innerHTML;
			}
			TWB.Processor.start("simulate","Modules.ReportOptions.Simulate_Launch","*",true);
			TWB.Modules.Villages.MakeUrl(Cvil,"place","sim");
		}
		else{
			alert(TWB.lang("gotoreport"));
		}
	},
	Simulate_Launch : function(){
		if(typeof TWB.simulate!="undefined"){
			var unit;
			for(unit in TWB.simulate[0]){
				TWB.Xpath("//input[@name='att_"+unit+"']",9).value=TWB.simulate[0][unit];
				TWB.Xpath("//input[@name='def_"+unit+"']",9).value=TWB.simulate[1][unit];
			}
			delete TWB.simulate;
		}
	},
	DeadReplace : function(){
		if(TWB.Modules.Icons.Screen()=="report" && TWB.Modules.Icons.Mode()=="all" && TWB.URL().match(/view=/)!=null){
			// Check if we are attacking / defending
			var WN=TWB._window();
			if(typeof WN.wrappedJSObject!="undefined"){
				WN=WN.wrappedJSObject;
			}
			var myId=WN.game_data.player.id;
			var Or=TWB.Xpath("//a[contains(@href,'info_village')]",6)
			for(i=0; i<Or.length; i++){
				var player=null;
				try{
					player=Or[i].parentNode.parentNode.previousSibling.previousSibling.childNodes[1].childNodes[0].href.match(/id=(\d+)/)[1];
				}catch(e){}
				if(player==myId){
					break;
				}
			}
			try{
				Or=Or[i].href.match(/id=(\d+)/)[1];
				var T=(i==0)?"att":"def";
				var IMG=TWB.Xpath("//table[@id='attack_info_"+T+"_units']//img[contains(@src,'unit_')]",6);
				var LOS=TWB.Xpath("//table[@id='attack_info_"+T+"_units']//tr[3]/td",6);
			}catch(e){
				try{
					var e=TWB.Xpath("//a[contains(@href,'info_village')]",9);
					var t=e.parentNode.parentNode.nextSibling.nextSibling.childNodes[1].childNodes[0].href.match(/id=(\d+)/);
					if(typeof t[1]!="undefined"){
						Or=t[1];
						var IMG=TWB.Xpath("//img[contains(@src,'unit_')]",6);
						var LOS=TWB.Xpath("//td[@class='nopad']//table[@class='vis']//table[@class='vis']//tr[3]/td",6);
					}
					else{
						return;
					}
				}
				catch(e){
					return;
				}
			}
			if(TWB.gei("TWB-v"+Or)){
				TWB.deadreplace={};
				var i;
				var flag=false;
				var fgoto="";
				for(i=0; i<IMG.length; i++){
					var unit=IMG[i].src.match(/unit_(\w+)/)[1];
					var losses=LOS[i+1].textContent;
					if(losses>0){
						flag=true;
						switch(unit){
							case 'spear':
							case 'sword':
							case 'axe':
							case 'archer':
								goto='barracks';
							break;
							
							case 'spy':
							case 'light':
							case 'marcher':
							case 'heavy':
								goto='stable';
							break;
							
							case 'ram':
							case 'catapult':
								goto='garage';
							break;
						}
						if(fgoto==""){
							fgoto=goto;
						}
						if(typeof TWB.deadreplace[goto]=="undefined"){
							TWB.deadreplace[goto]=[];
						}
						TWB.deadreplace[goto].push([unit,losses]);
					}
				}
				if(flag){
					TWB.Processor.start("dead_replace","Modules.ReportOptions.DeadReplace_Launch","*",false);
					TWB.Modules.Villages.MakeUrl(Or,fgoto);
				}
				else{
					alert(TWB.lang("nodead"));
				}
			}
			else{
				alert(TWB.lang("vilnotyours"));
			}
		}
		else{
			alert(TWB.lang("gotoreport"));
		}
	},
	DeadReplace_Launch : function(){
		if(typeof TWB.deadreplace!="undefined"){
			if(TWB.deadreplace.length<1){	
				TWB.Processor.stop("dead_replace");
				delete TWB.deadreplace;
			}
			else{
				var recruits=TWB.deadreplace[TWB.Modules.Icons.Screen()];
				if(typeof recruits!="undefined"){
					// Try to recruit
					var i;
					for(i=0; i<recruits.length; i++){
						var unit=recruits[i][0];
						var count=recruits[i][1];
						TWB.Xpath("//input[@name='"+unit+"']",9).value=count;
					}
					delete TWB.deadreplace[TWB.Modules.Icons.Screen()];
					TWB.Xpath("//input[@type='submit']",9).click();
				}
				else{
					// Move to next or stop
					var ngoto=null;
					for(ngoto in TWB.deadreplace){
						break;
					}
					if(ngoto){
						TWB.Modules.Icons.GoTo(ngoto);
					}
					else{
						TWB.Processor.stop("dead_replace");
						delete TWB.simulate;
					}
				}
			}
		}
	},
});